<?php

class ReturngoodsAction extends CommonAction
{

    /*
      thstatus 退货类型
      0.默认/取消
      1.申请退货
      2.审核通过，请提供客户信息
      3.用户已经提交物流信息，待退货
      4.后台确认，退货完成
    */

    public function _initialize()
    {
        parent::_initialize();
        $this->suppierids = M('Supplier_user')->Field('supplier_id')->where(array('user_id' => session(C('USER_AUTH_KEY'))))->buildSql();
        $this->is_suppierids = M('Supplier_user')->Field('supplier_id')->where(array('user_id' => session(C('USER_AUTH_KEY'))))->count();
    }

    function index(){
//        thstatus 退货类型
//        0.默认/取消
//        1.申请退货
//        2.审核通过，请提供客户信息
//        3.用户已经提交物流信息，待退货
//        4.后台确认，退货完成
//        5.管理员确认收退货，
        $myid=D('User')->where(' id = '.session(C('USER_AUTH_KEY')))->getField('member_id');
        $this->levelnum=D('Member')->where('id = '.$myid)->getField('level');
		$map = "o.site_id = ".$this->siteid." and s.thstatus <> 0";
        if (!session(C('ADMIN_AUTH_KEY'))&&$this->levelnum == 1){
            $supplierid=M('Supplier')->where('member_id = '.$myid)->getField('id');
            $map .=" and s.supplier_id = ".$supplierid;
        }else{
            if ($this->is_suppierids > 0) {
                $map .= " and s.supplier_id in(".$this->suppierids.")";
            }
        }
        if($_GET['keywords']!=''){
            $map.=' and o.ordernumber like "%'.$_GET['keywords'].'%"';
            $this->keywords=$_GET['keywords'];
        }
        $saleDB = D('sale');
        $count = $saleDB->alias('s')->join('INNER JOIN __ORDER__ as o on o.id = s.order_id')->where($map)->getField('count(distinct(s.id)) as tp');
        //实例化分页类
        import('ORG.Util.Page');
        $p = new Page($count, 20);
        $data = $saleDB->alias('s')->join('LEFT JOIN __ORDER__ as o on o.id = s.order_id')->where($map)->order('create_time DESC')->limit($p->firstRow . ',
' . $p->listRows)->Field('s.id as sale_id,s.*, o.id,o.ordernumber,o.yourname')->select();
        $pages = $p->show();
        $this->assign('list', $data);
        $this->assign('pages', $pages);
        $this->display();
    }

    //退货进度表
    public function returngoods_mes()
    {

        $sale_id = $this->_get("id");
        $returngoodsDB = D('Returngoods');
        $data = $returngoodsDB->_list(array('sale_id' => $sale_id), '', 20);
        $this->assign('list', $data['list']);
        $this->assign('pages', $data['pages']);

        //收货人信息

        $sale = M('sale')->where(array('id' => $sale_id))->find();
        $order_id = $sale['order_id'];
        // $info = $sale['order_id'];
        $this->assign('sale_goods', $sale);
        $info = M('order')->where(array('id' => $order_id))->find();
        $this->assign('info', $info);

        //退货商品
        $product_id = $this->_get('product_id');
        $product = M('product')->where(array('id' => $product_id))->find();
        $this->assign('product', $product);
        $this->display();


    }


    public function upstate()
    {
        $sale_id = $this->_post('sale_id', 'int') > 0 ? $this->_post('sale_id', 'int') : 0;
        $OrderDB = D('Order');
        $SaleDB = D('Sale');
        //$sale_id = $this->_get('sale_id');
        $sale_goods = $SaleDB->where(array('id' => $sale_id))->find();
        if (IS_POST) {
            /*退货数量和退货理由*/
            $remark = $this->_post('remark');
            $num = $sale_goods['thnum'];
            $thtype = $sale_goods['thtype'];

            if ($sale_id == 0){
                $this->error('换货商品不存在！');
                return;
            }
            $thstatus = $SaleDB->where(array('id' => $sale_id))->getField('thstatus');
            if ($thstatus == 1) {
                $thstatus = 2;
            }
            if ($thstatus == 3) {
                if ($sale_goods['thtype'] == 1) {
                    //退货
                    $thstatus = 4;
                } elseif($sale_goods['thtype'] == 2) {
                    $thstatus = 5;
                    $shipping['couriernumber'] = $this->_post('couriernumber');
                    $shipping['courier'] = $this->_post('courier');
                    $remark = '商户寄回商品，【' . $shipping['courier'] . '】物流单号：' . $shipping['couriernumber'];
                }else{
				    $remark = '没有发货，用户退款！';
					$thstatus = 4;
				}
            }
			if(isset($_POST['thstatus'])){
				$thstatus = intval($_POST['thstatus']);
			}
            $ret = $OrderDB->updatethstatus($sale_goods['order_id'], $sale_id, $this->siteid, $thstatus, $num, $thtype, $remark, $shipping);
            if ($ret) {
                $this->success('操作成功');
            } else {
                $this->error($OrderDB->errorinfo);
            }
            return;
        }
    }


    /*
      thstatus 换货类型
      0.默认/取消
      1.申请换货
      2.审核通过，请提供客户信息
      3.用户已经提交物流信息，待退货
      4.后台确认，退货完成

    */

    //换货处理
    public function exchangegoods()
    {
        $sale_id = $this->_get('sale_id', 'int') > 0 ? $this->_get('sale_id', 'int') : 0;
        $SaleDB = D('Sale');
        //$sale_id = $this->_get('sale_id');
        $sale_goods = $SaleDB->where(array(
            'id' => $sale_id
        ))->find();
        $this->assign('sale_goods', $sale_goods);
        $this->display();
    }
}
